package torres.item.oneshot;

import java.util.ArrayList;
import java.util.List;

import torres.entity.Entity;
import torres.fileIO.BasicContainer;
import torres.fileIO.ClassContainer;
import torres.item.Item;
import torres.action.Action;

/**
 * @author Torres
 * An item that is always consumed on touch, even if it has no
 * effect on the usee.
 */

public class OneShotItem extends Item
{
	List<Action> actionList;
	
	/**
	 * Create a new OneShotItem with the specified name and no Effects.
	 * @param name The name of this OneShotItem.
	 */
	public OneShotItem(String name, int id) {
		super(name, id);
		super.setConsumed();
		this.setActions(new ArrayList<Action>());
	}
	
	/**
	 * Creates a new OneShotItem with the specified name and Effects.
	 * @param name The name of this OneShotItem.
	 * @param effects The Effects to be applied on touch.
	 */
	public OneShotItem(String name, int id, List<Action> effects) {
		
		this(name, id);
		this.setActions(effects);
	}
	
	/**
	 * Creates a new OneShotItem that is a copy of the specified OneShotItem.
	 * @param item The OneShotItem to be copied.
	 */
	public OneShotItem(OneShotItem item) {
		this(item.getName(), item.getID(), item.getActions());
	}
	
	/**
	 * Triggers the effect of an Entity walking on this item.
	 * @param e The Entity that is walking on this item
	 */
	public void onTouch(Entity entity) {
		
		for(Action a : this.getActions()) {
			a.setUp(entity);
			Thread t = new Thread(a);
			t.setName("OneShotItem");
			t.start();
		}
	}
	
	
	/**
	 * Get the list of Effects that occur on touch.
	 * @return The list of Effects that occur on touch.
	 */
	private List<Action> getActions() {
		
		return this.actionList;
	}
	
	/**
	 * Set the list of Effects that occur on touch.
	 * @param effects The list of Effects to occur on touch.
	 */
	private void setActions(List<Action> actions) {
		
		this.actionList = actions;
	}
	
	/**
	 * Adds the specified Effect to this OneShotItem.
	 * @param effect The Effect to be added to this OneShotItem.
	 */
	public void addEffect(Action action) {
		
		actionList.add(action);
	}

	/**
	 * Adds the specified Effects to this OneShotItem.
	 * @param effect The Effects to be added to this OneShotItem.
	 */
	public void appendEffectList(List<Action> actionList)
	{
		this.actionList.addAll(actionList);
	}

	/* (non-Javadoc)
	 * @see torres.item.Item#save()
	 */
	@Override
	public ClassContainer save() {
		ClassContainer curClass = super.save();
		curClass.addProperty(new BasicContainer("type", "OneShotItem"));
		return curClass;
	}

	/* (non-Javadoc)
	 * @see torres.item.Item#load(torres.fileIO.ClassContainer)
	 */
	@Override
	public void load(ClassContainer classContainer) {
		// TODO Auto-generated method stub
		
	}
}
